home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / rcs55.zip / RCSMERGE.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  8KB  |  260 lines

  1. /*
  2.  *                       rcsmerge operation
  3.  */
  4. /*****************************************************************************
  5.  *                       join 2 revisions with respect to a third
  6.  *****************************************************************************
  7.  */
  8.  
  9. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  10.    Copyright 1990 by Paul Eggert
  11.    Distributed under license by the Free Software Foundation, Inc.
  12.  
  13. This file is part of RCS.
  14.  
  15. RCS is free software; you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation; either version 1, or (at your option)
  18. any later version.
  19.  
  20. RCS is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. GNU General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with RCS; see the file COPYING.  If not, write to
  27. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  28.  
  29. Report problems and direct all questions to:
  30.  
  31.     rcs-bugs@cs.purdue.edu
  32.  
  33. */
  34.  
  35.  
  36.  
  37. /* $Log: rcsmerge.c%v $
  38.  * Revision 1.2  1991/08/23  13:36:48  SGP
  39.  * Ported to MSDOS using Borland C++
  40.  *
  41.  * Revision 5.3  1990/11/01  05:03:50  eggert
  42.  * Remove unneeded setid check.
  43.  *
  44.  * Revision 5.2  1990/09/04  08:02:28  eggert
  45.  * Check for I/O error when reading working file.
  46.  *
  47.  * Revision 5.1  1990/08/29  07:14:04  eggert
  48.  * Add -q.  Pass -L options to merge.
  49.  *
  50.  * Revision 5.0  1990/08/22  08:13:41  eggert
  51.  * Propagate merge's exit status.
  52.  * Remove compile-time limits; use malloc instead.
  53.  * Make lock and temp files faster and safer.  Ansify and Posixate.  Add -V.
  54.  * Don't use access().  Tune.
  55.  *
  56.  * Revision 4.5  89/05/01  15:13:16  narten
  57.  * changed copyright header to reflect current distribution rules
  58.  * 
  59.  * Revision 4.4  88/08/09  19:13:13  eggert
  60.  * Beware merging into a readonly file.
  61.  * Beware merging a revision to itself (no change).
  62.  * Use execv(), not system(); yield exit status like diff(1)'s.
  63.  * 
  64.  * Revision 4.3  87/10/18  10:38:02  narten
  65.  * Updating version numbers. Changes relative to version 1.1 
  66.  * actually relative to 4.1
  67.  * 
  68.  * Revision 1.3  87/09/24  14:00:31  narten
  69.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  70.  * warnings)
  71.  * 
  72.  * Revision 1.2  87/03/27  14:22:36  jenkins
  73.  * Port to suns
  74.  * 
  75.  * Revision 4.1  83/03/28  11:14:57  wft
  76.  * Added handling of default branch.
  77.  * 
  78.  * Revision 3.3  82/12/24  15:29:00  wft
  79.  * Added call to catchsig().
  80.  *
  81.  * Revision 3.2  82/12/10  21:32:02  wft
  82.  * Replaced getdelta() with gettree(); improved error messages.
  83.  *
  84.  * Revision 3.1  82/11/28  19:27:44  wft
  85.  * Initial revision.
  86.  *
  87.  */
  88. #include "rcsbase.h"
  89.  
  90. static exiting void nowork P((void));
  91.  
  92. static const char co[] = CO;
  93.  
  94. mainProg(rcsmergeId, "rcsmerge", "$Id: rcsmerge.c%v 1.2 1991/08/23 13:36:48 SGP Exp $")
  95. {
  96.     static const char cmdusage[] =
  97.         "\nrcsmerge usage: rcsmerge -rrev1 [-rrev2] [-p] [-Vn] file";
  98.     static const char quietarg[] = "-q";
  99.  
  100.     const char *rev1, *rev2; /*revision numbers*/
  101.     const char *temp1file, *temp2file;
  102.     const char *expandarg, *versionarg;
  103.     const char *mergearg[13], **a;
  104.         int tostdout;
  105.     int status, workfd;
  106.     struct buf commarg;
  107.     struct buf numericrev; /* holds expanded revision number */
  108.     struct hshentries *gendeltas; /* deltas to be generated */
  109.         struct hshentry * target;
  110.  
  111.     initid();
  112.         catchints();
  113.  
  114.     bufautobegin(&commarg);
  115.     bufautobegin(&numericrev);
  116.     rev1 = rev2 = nil;
  117.     status = 0; /* Keep lint happy.  */
  118.     tostdout = false;
  119.     expandarg = versionarg = quietarg; /* i.e. a no-op */
  120.  
  121.         while (--argc,++argv, argc>=1 && ((*argv)[0] == '-')) {
  122.                 switch ((*argv)[1]) {
  123.                 case 'p':
  124.                         tostdout=true;
  125.             goto revno;
  126.         case 'q':
  127.             quietflag = true;
  128.                         /* falls into -r */
  129.                 case 'r':
  130.         revno:
  131.                         if ((*argv)[2]!='\0') {
  132.                 if (!rev1)
  133.                     rev1 = *argv + 2;
  134.                 else if (!rev2)
  135.                     rev2 = *argv + 2;
  136.                 else
  137.                                     faterror("too many revision numbers");
  138.                         } /* do nothing for empty -r or -p */
  139.                         break;
  140.         case 'V':
  141.             versionarg = *argv;
  142.             setRCSversion(versionarg);
  143.             break;
  144.  
  145.         case 'k':
  146.             expandarg = *argv;
  147.             if (0 <= str2expmode(expandarg+2))
  148.                 break;
  149.             /* fall into */
  150.                 default:
  151.             faterror("unknown option: %s%s", *argv, cmdusage);
  152.                 };
  153.         } /* end of option processing */
  154.  
  155.     if (argc<1) faterror("no input file%s", cmdusage);
  156.     if (!rev1) faterror("no base revision number given");
  157.  
  158.         /* now handle all filenames */
  159.  
  160.     if (pairfilenames(argc, argv, rcsreadopen, true, false) == 1) {
  161.  
  162.                 if (argc>2 || (argc==2&&argv[1]!=nil))
  163.                         warn("too many arguments");
  164.         diagnose("RCS file: %s\n", RCSfilename);
  165.         if ((workfd = open(workfilename, tostdout?O_RDONLY:O_RDWR)) < 0)
  166.             nowork();
  167.  
  168.                 gettree();  /* reads in the delta tree */
  169.  
  170.                 if (Head==nil) faterror("no revisions present");
  171.  
  172.  
  173.         if (!expandsym(rev1,&numericrev)) goto end;
  174.         if (!(target=genrevs(numericrev.string, (char *)nil, (char *)nil, (char *)nil,&gendeltas))) goto end;
  175.                 rev1=target->num;
  176.         if (!rev2)
  177.             rev2  =  Dbranch ? Dbranch : Head->num;
  178.         if (!expandsym(rev2,&numericrev)) goto end;
  179.         if (!(target=genrevs(numericrev.string, (char *)nil, (char *)nil, (char *)nil,&gendeltas))) goto end;
  180.                 rev2=target->num;
  181.  
  182.         if (strcmp(rev1,rev2) == 0) {
  183.             error("merging revision %s to itself (no change)",
  184.                 rev1
  185.             );
  186.             if (tostdout) {
  187.                 FILE *w;
  188.                 errno = 0;
  189.                 if (!(w = fdopen(workfd,"r")))
  190.                     nowork();
  191.                 fastcopy(w,stdout);
  192.                 ffclose(w);
  193.             }
  194.             goto end;
  195.         }
  196.         if (close(workfd) < 0)
  197.             nowork();
  198.  
  199.         temp1file = maketemp(0);
  200.         temp2file = maketemp(1);
  201.  
  202.         diagnose("retrieving revision %s\n", rev1);
  203.         bufscpy(&commarg, "-p");
  204.         bufscat(&commarg, rev1);
  205.         if (run((char*)nil,temp1file, co,quietarg,commarg.string,expandarg,versionarg,RCSfilename,(char*)nil)){
  206.                         faterror("co failed");
  207.                 }
  208.         diagnose("retrieving revision %s\n",rev2);
  209.         bufscpy(&commarg, "-p");
  210.         bufscat(&commarg, rev2);
  211.         if (run((char*)nil,temp2file, co,quietarg,commarg.string,expandarg,versionarg,RCSfilename,(char*)nil)){
  212.                         faterror("co failed");
  213.                 }
  214.         diagnose("Merging differences between %s and %s into %s%s\n",
  215.                          rev1, rev2, workfilename,
  216.                          tostdout?"; result to stdout":"");
  217.  
  218.         a = mergearg;
  219.         *a++ = nil;
  220.         *a++ = nil;
  221.         *a++ = MERGE;
  222.         if (tostdout)
  223.             *a++ = "-p";
  224.         if (quietflag)
  225.             *a++ = quietarg;
  226.         *a++ = "-L";  *a++ = workfilename;
  227.         *a++ = "-L";  *a++ = rev2;
  228.         *a++ = workfilename;
  229.         *a++ = temp1file;
  230.         *a++ = temp2file;
  231.         *a = nil;
  232.  
  233.         status = runv(mergearg);
  234.         if (!WIFEXITED(status)  ||  1 < WEXITSTATUS(status)) {
  235.                         faterror("merge failed");
  236.                 }
  237.         }
  238.  
  239. end:
  240.     tempunlink();
  241.     exitmain(nerror ? EXIT_TROUBLE : WEXITSTATUS(status));
  242. }
  243.  
  244. #if lint
  245. #    define exiterr rmergeExit
  246. #endif
  247.     exiting void
  248. exiterr()
  249. {
  250.     tempunlink();
  251.     _exit(EXIT_TROUBLE);
  252. }
  253.  
  254.  
  255.     static exiting void
  256. nowork()
  257. {
  258.     efaterror(workfilename);
  259. }
  260.